<?php
namespace Admin\Controller;
use Think\Controller;
class TypeController extends AdminBaseController 
{	
	/*
	 * 继承父类构造函数 分区管理
	 */
	public function __construct(){
		parent::__construct();
		$this->type = D("Type");
	}


	/*
	 * 显示分区
	 */
	public function index(){
		$where['title'] = array("like","%".$_GET['title']."%");
        $where['aid'] = array("like","%".$_GET['aid']."%");
        //实例化一个new模型
        $count      = $this->type->where($where)->count();// 查询满足要求的总记录数
        //echo $this->new->getLastSql();
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        // $Page -> setConfig('header','共%TOTAL_ROW%条');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $show       = $Page->show();// 分页显示输出
        $res = $this->type->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
    	//$this的使用
        $this->assign("res",$res);
    	$this->assign("page",$show); 
    	// var_dump($res);
    	$this->display();
	}

	/*
	 * 添加分区
	 */
	public function add(){
		if(IS_GET){
			$this->display();
		}else{
			$upfile = $this->upload();
			$path = "/Uploads/".$upfile['upfile']['savepath'].$upfile['upfile']['savename'];
			$this->user->create(); //创建对象数据
			$this->user->password = md5(I("post.password"));
			$this->user->lastttime = date("Y-m-d H:i:s");
			$this->user->photo = $path;
			if($this->user->add()){
					$this->success("管理员添加成功");
				}else{
					$this->error("管理员添加失败");
				}
			
		}
	}

	public function upload(){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
		$upload->savePath  =     ''; // 设置附件上传（子）目录
		// 上传文件
		$info   =   $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError()); 
		}else{// 上传成功
			return $info;
		}
	}

	/*
	 * 删除分区
	 */
	public function del($id){
		if($this->user->del($id)){
			$data['error'] = 1;
		}else{
			$data['error'] = 0;	
		}
		//注意返回的格式
		$this->ajaxReturn($data);
	}

	/*
	 * 编辑分区
	 */
	public function edit(){

	}
}